package com.example.day02;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day02.p.Persenter;
import com.example.day02.v.View_Main;

import java.util.ArrayList;
import java.util.List;

public class MainActivity4 extends AppCompatActivity implements View.OnClickListener, View_Main {

    private EditText mEdtext;
    private Button mButton;
    private RecyclerView mRecy;
    private ArrayList<Bean> beans;
    private Recy_Adapter adapter;
    private Persenter persenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        initView();
        persenter = new Persenter(this);

    }

    private void initView() {
        mEdtext = (EditText) findViewById(R.id.edtext);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(this);
        mRecy = (RecyclerView) findViewById(R.id.recy);
        mRecy.setLayoutManager(new LinearLayoutManager(this));
        beans = new ArrayList<>();
        adapter = new Recy_Adapter(this, beans);
        mRecy.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                String trim = mEdtext.getText().toString().trim();
                persenter.upDate(trim);
                break;
            default:
                break;
        }
    }

    @Override
    public void showData(List<Bean> resultsBeans) {
        beans.addAll(resultsBeans);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(this, "str", Toast.LENGTH_SHORT).show();
    }
}
